package com.lsh.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.lsh.entity.Response;
import com.lsh.entity.Token;
import com.lsh.entity.templatemessage.Keyworld;
import com.lsh.entity.templatemessage.WxTemplateMessage;
import com.lsh.service.WXService;
import com.lsh.util.HttpClient;
import com.lsh.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/10/18 5:33 下午
 * @desc ：
 */
@Slf4j
@Service
public class WXServiceImpl implements WXService {
    @Autowired
    RedisUtils redisUtils;

    public static final String WTY_OPEN_ID = "oCV_T6FlZ4U0EEhNlVi51udzQI8U";
    public static final String LSH_OPEN_ID = "oCV_T6IutImdnwuD658EyIMM_goU";
    /**
     *     凭证获取（GET）
     */
    public final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    private static String appId = "wx4959a20a2a31b3fc";
    private static String appsecret = "d0e97bd7be270bc400734e385eb1ad0e";

    public String getToken(){
        String access_token = null;
        access_token = (String) redisUtils.get("wx_access_token");
        if (access_token == null){
            String requestUrl = token_url.replace("APPID", appId).replace("APPSECRET", appsecret);
            Response response = HttpClient.doGet(requestUrl);
            Token token = JSONObject.parseObject(response.getRespContent(), Token.class);
            access_token = token.getAccess_token();
            redisUtils.set("wx_access_token",access_token,7200);
        }
        return access_token;
    }

    public void sendWXMessage(String name,String datetime){
        String access_token = getToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        String replaceUrl = url.replace("ACCESS_TOKEN", access_token);
        Keyworld keyworld1 = new Keyworld(name,"#FF2D2D");
        Keyworld keyworld2 = new Keyworld(datetime,"#173177");
        net.sf.json.JSONObject keyword11 = net.sf.json.JSONObject.fromObject(keyworld1);
        net.sf.json.JSONObject keyword21 = net.sf.json.JSONObject.fromObject(keyworld2);
        net.sf.json.JSONObject templateData = new net.sf.json.JSONObject();
        templateData.put("keyword1",keyword11);
        templateData.put("keyword2",keyword21);
        WxTemplateMessage wxTemplateMessage1 = new WxTemplateMessage(WTY_OPEN_ID, "BRQ5bqDQSaQUE41k32LY-oR4K11XTBscxWwoiSP39aw", null,null);
        WxTemplateMessage wxTemplateMessage2 = new WxTemplateMessage(LSH_OPEN_ID, "BRQ5bqDQSaQUE41k32LY-oR4K11XTBscxWwoiSP39aw", null,null);
        net.sf.json.JSONObject msgData1 = net.sf.json.JSONObject.fromObject(wxTemplateMessage1);
        net.sf.json.JSONObject msgData2 = net.sf.json.JSONObject.fromObject(wxTemplateMessage2);
        msgData1.put("data",templateData.toString());
        msgData2.put("data",templateData.toString());
        log.info("微信模板发送的数据参数："+msgData1);
        String post1 = HttpClient.doPOST(replaceUrl, "POST", msgData1.toString());
        String post2 = HttpClient.doPOST(replaceUrl, "POST", msgData2.toString());
        log.info("postResponse："+post1);
        log.info("postResponse："+post2);
    }

}
